<?php
class Portfolio_model extends Model
{

	function __construct()
	{
		parent::factory();
	}


	function add($data)
	{
		$title = parent::$db->escape($data['title']);
		$body = parent::$db->escape($data['body']);
		$thumb = parent::$db->escape($data['thumb']);
		$pic = parent::$db->escape($data['large']);
		$rss = (int) $data['rss'];
		$date = time();

		parent::$db->query("INSERT INTO t_portfolio(title, body, rss, thumb, picture, date) VALUES('$title', '$body', $rss, '$thumb', '$pic', $date)");

		return;
	}

	function edit($data)
	{
		$title = parent::$db->escape($data['title']);
		$body = parent::$db->escape($data['body']);
		$rss = (int) $data['rss'];
		$id = (int) $data['id'];
		
		$images_sql = '';
		$images_sql_array = array();
		if ($data['thumb'])
			$images_sql_array[] = "thumb = '".$data['thumb']."'";
		if ($data['large'])
			$images_sql_array[] = "picture = '".$data['large']."'";
		
		if ($images_sql_array[0])
			$images_sql = implode(',', $images_sql_array);
			
		
			
			

		parent::$db->query("UPDATE t_portfolio SET title = '$title', body = '$body', rss = $rss ".(($images_sql) ? ', '.$images_sql: '')." WHERE id = $id");
	}

	function delete($ids)
	{
		if (is_array($ids))
			$ids = implode(',', $ids);
		else
			$ids = (int) $ids;


		parent::$db->query("DELETE FROM t_portfolio WHERE id IN ($ids)");

		return;
	}

	function getItems($rss = 0, $limit = 0)
	{
		return parent::$db->getAll("SELECT * FROM t_portfolio ".($rss ? 'WHERE rss = 1' : '')." ORDER BY id DESC ".($limit ? 'LIMIT 0,'.$limit : '')."");
	}


	function getItem($id)
	{
		return parent::$db->getRow("SELECT * FROM t_portfolio WHERE id = $id");
	}
	
	
	function getImageFile($ids, $field)
	{
		if (is_array($ids))
			$ids = implode(',', $ids);
		else
			$ids = (int) $ids;
			
		return parent::$db->getRow("SELECT $field FROM t_portfolio WHERE id IN ($ids)");
	}


	function __destruct(){}
}
?>